package TUILayer;

import java.util.Scanner;
import ControlLayer.*;
import ModelLayer.*;
import java.util.ArrayList;

/**
 * Write a description of class MainMenu here.
 * 
 * @author (Group A) 
 * @version (2014.11.03)
 */
public class DVDUI
{
    private DVDHandler dvds;
    private int choise;

    public DVDUI()
    {
        dvds = new DVDHandler();
    }

    public void dvdMenuStart()
    {
        boolean exit=false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writedvdMenu();

            switch (choise) 
            {
                case 1: startCreateDVD();
                break;
                case 2: startFindDVD();
                break;
                case 3: startUpdateDVD();
                break;
                case 4: startCreateCopy();    
                break;
                case 5: startDeleteDVD();
                break;
                case 6: exit = true;
                break;
            }
        }//end while
    }//end start

    private int writedvdMenu()
    {       
        // creates a keyboard object to read input
        try
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** DVD Collection ***");
            System.out.println(" (1) Add new DVD");
            System.out.println(" (2) Find a DVD");
            System.out.println(" (3) Change DVD Information");
            System.out.println(" (4) Add a copy of a DVD");
            System.out.println(" (5) Delete DVD");
            System.out.println(" (6) Back");
            System.out.print("\n Choose an option: ");

            choise = keyboard.nextInt();
        } 
        catch(Exception e)
        {
            System.out.println("Error!");
            pause();
            dvdMenuStart();
        }

        return choise;
    }

    private void startCreateDVD()
    {
        try
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Fill in the information of the new DVD: ");
            System.out.println("Barcode:");
            int barcode = Integer.parseInt(keyboard.nextLine());
            System.out.println("Title:");
            String title = keyboard.nextLine();
            System.out.println("Artist:");
            String artist = keyboard.nextLine();
            System.out.println("Publication Date:");
            String publicationDate = keyboard.nextLine();
            DVD dvd = new DVD(barcode,title,artist,publicationDate);
            if(dvds.addDVD(dvd))
            {
                dvds.addDVD(dvd);
                System.out.println("New DVD has been added!");
                createCopy(title);
            } 
            else
            {
                System.out.println("A DVD with the same barcode/name already exists! Try another!");
            }
        } 
        catch(Exception e)
        {
            System.out.println("Something went wrong");
        }
        pause();
    }

    private void startFindDVD()
    {
        Scanner keyboard = new Scanner(System.in);
        try
        {
            System.out.println("Enter the title of the DVD you are looking for:");
            String title = keyboard.nextLine();
            DVD dvd = dvds.findDVDByName(title);
            System.out.println("Barcode: " + dvd.getBarcode());
            System.out.println("Title: " + dvd.getTitle());
            System.out.println("Artist: " + dvd.getArtist());
            System.out.println("Publication date: " + dvd.getPublicationDate());

            ArrayList<Copy> copies = dvd.getCopyList();
            System.out.println("##### Copies of the DVD #####");
            for(int i=0; i<copies.size(); i++)
            {
                System.out.println("Serial number: " + copies.get(i).getSerialNumber());
                System.out.println("Date: " + copies.get(i).getPurchaseDate());
                System.out.println("Price: " + copies.get(i).getPurchasePrice());
                if(copies.get(i).getAvailable())
                {
                    System.out.println("Available: yes");
                }
                else 
                {
                    System.out.println("Available: no");
                }
                // add availability check
            }
        } 
        catch (Exception e) 
        {
            System.out.println("The DVD was not found!");
        }
        pause();
    }

    private void startUpdateDVD()
    {
        try
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter the barcode of the DVD:");
            int barcode = Integer.parseInt(keyboard.nextLine());
            System.out.println("Fill in the new information:");
            System.out.println("Title:");
            String title = keyboard.nextLine();
            System.out.println("Artist:");
            String artist = keyboard.nextLine();
            System.out.println("Publication Date:");
            String publicationDate = keyboard.nextLine();
            dvds.updateDVD(barcode,title,artist,publicationDate);
        } 
        catch (Exception e)
        {
            System.out.println("There was a problem updating the information");
        }
        pause();
    }

    private void startCreateCopy()
    {
        try
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter the title of the DVD you want to add copy of:");
            String title = keyboard.nextLine();
            System.out.println("Enter the information of the new copy:");
            System.out.println("Serial number:");
            String serialNumber = keyboard.nextLine();
            System.out.println("Date added:");
            String purchaseDate = keyboard.nextLine();
            System.out.println("Price:");
            String purchasePrice = keyboard.nextLine();
            dvds.addDVDCopy(title,serialNumber,purchaseDate,purchasePrice);
            //            if(dvds.addDVDCopy(title,serialNumber,purchaseDate,purchasePrice))
            //            {
            //                dvds.addDVDCopy(title,serialNumber,purchaseDate,purchasePrice);
            //                System.out.println("A copy has been added!");
            //            } else {
            //                System.out.println("A copy with the same serial number already exists! Try another!");
            //            }
        }
        catch(Exception e) 
        {
            System.out.println("Something went wrong");
        }
        pause();
    }

    private void createCopy(String title)
    {
        try
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter the information of the first copy:");
            System.out.println("Serial number:");
            String serialNumber = keyboard.nextLine();
            System.out.println("Date added:");
            String purchaseDate = keyboard.nextLine();
            System.out.println("Price:");
            String purchasePrice = keyboard.nextLine();
            dvds.addDVDCopy(title,serialNumber,purchaseDate,purchasePrice);
            //            if(dvds.addDVDCopy(title,serialNumber,purchaseDate,purchasePrice))
            //            {
            //                dvds.addDVDCopy(title,serialNumber,purchaseDate,purchasePrice);
            //                System.out.println("A copy has been added!");
            //            } else {
            //                System.out.println("A copy with the same serial number already exists! Try another!");
            //            }
        } 
        catch(Exception e) 
        {
            System.out.println("Something went wrong");
        }
        pause();
    }

    public void startDeleteDVD()
    {
        try 
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter the title of the DVD you want to delete");
            String title = keyboard.nextLine();
            dvds.deleteDVDByName(title);
            System.out.println("The DVD has been deleted!");
        } 
        catch(Exception e) 
        {
            System.out.println("There is no such DVD!");
        }
        pause();
    }

    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
}